package cn.iocoder.yudao.module.assess.controller.admin.template;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.assess.controller.admin.template.vo.*;
import cn.iocoder.yudao.module.assess.dal.dataobject.template.AssessTemplateDO;
import cn.iocoder.yudao.module.assess.convert.template.AssessTemplateConvert;
import cn.iocoder.yudao.module.assess.service.template.AssessTemplateService;

@Api(tags = "管理后台 - 指标模板")
@RestController
@RequestMapping("/assess/template")
@Validated
public class AssessTemplateController {

    @Resource
    private AssessTemplateService templateService;

    @PostMapping("/create")
    @ApiOperation("创建指标模板")
    @PreAuthorize("@ss.hasPermission('assess:template:create')")
    public CommonResult<Long> createTemplate(@Valid @RequestBody AssessTemplateCreateReqVO createReqVO) {
        return success(templateService.createTemplate(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新指标模板")
    @PreAuthorize("@ss.hasPermission('assess:template:update')")
    public CommonResult<Boolean> updateTemplate(@Valid @RequestBody AssessTemplateUpdateReqVO updateReqVO) {
        templateService.updateTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除指标模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('assess:template:delete')")
    public CommonResult<Boolean> deleteTemplate(@RequestParam("id") Long id) {
        templateService.deleteTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得指标模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('assess:template:query')")
    public CommonResult<AssessTemplateRespVO> getTemplate(@RequestParam("id") Long id) {
        AssessTemplateDO template = templateService.getTemplate(id);
        return success(AssessTemplateConvert.INSTANCE.convert(template));
    }

    @GetMapping("/list")
    @ApiOperation("获得指标模板列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('assess:template:query')")
    public CommonResult<List<AssessTemplateRespVO>> getTemplateList(@RequestParam("ids") Collection<Long> ids) {
        List<AssessTemplateDO> list = templateService.getTemplateList(ids);
        return success(AssessTemplateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得指标模板分页")
    @PreAuthorize("@ss.hasPermission('assess:template:query')")
    public CommonResult<PageResult<AssessTemplateRespVO>> getTemplatePage(@Valid AssessTemplatePageReqVO pageVO) {
        PageResult<AssessTemplateDO> pageResult = templateService.getTemplatePage(pageVO);
        return success(AssessTemplateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出指标模板 Excel")
    @PreAuthorize("@ss.hasPermission('assess:template:export')")
    @OperateLog(type = EXPORT)
    public void exportTemplateExcel(@Valid AssessTemplateExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AssessTemplateDO> list = templateService.getTemplateList(exportReqVO);
        // 导出 Excel
        List<AssessTemplateExcelVO> datas = AssessTemplateConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "指标模板.xls", "数据", AssessTemplateExcelVO.class, datas);
    }

}
